﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Xml;
using Microsoft.ApplicationBlocks.Data;
using MCMLXVII.BackEnd.Data.SystemDatabase.DataSets;
using System.ServiceModel;

#endregion

namespace MCMLXVII.BackEnd.Data.SystemDatabase
{
    public class Security
    {

        public enum AccessLevel
        { 
            Allowed = 0,
            ToolNotFound = 1,
            ExplicitDenyFound = 2,
            InsufficientRights = 3
        }

        public Security()
        {

        }

        public static DSSecurityProviders.SecurityProvidersAndClassRow GetDomainInfo(string DomainNetbiosName)
        {
            DSSecurityProviders.SecurityProvidersAndClassDataTable dt = new DSSecurityProviders.SecurityProvidersAndClassDataTable();
            dt.Load(SqlHelper.ExecuteReader("GetDomainInfo", new object[] { DomainNetbiosName }));
            if (dt.Rows.Count > 0)
                return (DSSecurityProviders.SecurityProvidersAndClassRow)dt.Rows[0];
            else
                return null;
        }

        public static DSSecurityProviders.SecurityProvidersAndClassRow GetDomainInfo(int SecurityProviderID)
        {
            DSSecurityProviders.SecurityProvidersAndClassDataTable dt = new DSSecurityProviders.SecurityProvidersAndClassDataTable();
            dt.Load(SqlHelper.ExecuteReader("GetDomainInfoByID", new object[] { SecurityProviderID }));
            if (dt.Rows.Count > 0)
                return (DSSecurityProviders.SecurityProvidersAndClassRow)dt.Rows[0];
            else
                return null;
        }


        public static DSSecurityProviders.SecurityProvidersAndClassDataTable GetEnabledSecurityProviders(int MemberID)
        {
            DSSecurityProviders.SecurityProvidersAndClassDataTable dt = new DSSecurityProviders.SecurityProvidersAndClassDataTable();
            dt.Load(SqlHelper.ExecuteReader("GetEnabledSecurityProviders", MemberID));
            return dt;
        }


        public static DSMembers.OrganizationMembersAndClassDataTable GetMembersInfo(int SecProviderID, string LogonName)
        {
            DSMembers.OrganizationMembersAndClassDataTable dt = new DSMembers.OrganizationMembersAndClassDataTable();
            dt.Load(SqlHelper.ExecuteReader("GetOrganizationMemberInfo", new object[] { SecProviderID, LogonName }));
            if (dt.Rows.Count > 0)
                return dt;
            else
                return null;
        }

        public static AccessLevel CheckPermissionForTool(int MemberID, string ToolName)
        {
            return  (AccessLevel)SqlHelper.ExecuteScalar("ValidateUserAccessToTool", new object[] { MemberID, ToolName });           
        }

        public static String[] GetSecurityDescriptor(int MemberID, string ToolName)
        {
            DbDataReader sr = SqlHelper.ExecuteReader("GetPermissionsForToolAndMember", new object[] { MemberID, ToolName });
            List<string> ls = new List<string>();
            while (sr.Read())
            {
                ls.Add(sr.GetString(0));
            }
            return ls.ToArray();
        }

        public static bool CheckActionAccess(int MemberID, string PermissionName)
        {
            DbDataReader sr = SqlHelper.ExecuteReader("CheckAccessForMember", new object[] { MemberID, PermissionName });
            if (sr.Read())
                return (sr.GetChar(0) == 'Y');
            else
                return false;
        }

        private static Hashtable Sesiones = new Hashtable();


        public static void CreateNewSession(int MemberID, string SecurityDescriptor)
        {

            lock (Sesiones)
            {
                Sesiones.Add(OperationContext.Current.SessionId, MemberID);
            
            }
            // TO DO: Ver si vale la pena guardarlo en la bdd 
            //return (Guid)SqlHelper.ExecuteScalar("CreateNewSession", new object[] {MemberID, LeaseDuration, Evidence, SecurityDescriptor   });
        
            

        }

        public static int GetMemberIDFromSession()
        {
            lock (Sesiones)
            {
                return (int)Sesiones[OperationContext.Current.SessionId];
            }
        
        }



        public static DSSession.SessionsRow GetSessionInfo()
        {

            //no estamos grabando por ahora
            DbDataReader sr = SqlHelper.ExecuteReader("GetSessionInfo", new object[] { OperationContext.Current.SessionId });
            DSSession.SessionsDataTable dt = new DSSession.SessionsDataTable();
            dt.Load(sr);
            if (dt.Rows.Count < 1)
                return null;
            else
            {
                //XmlDocument xdoc = new XmlDocument();
                //DSSession.SessionsRow SessionInfo = (DSSession.SessionsRow)dt.Rows[0];
                //xdoc.Load(SessionInfo.SecurityDescriptor);

                return (DSSession.SessionsRow)dt.Rows[0];
            }
        }

    }
}
